package com.eostoken.eos_wallet.ui.activity;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.MapUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseActivity;
import com.eostoken.eos_wallet.bean.MortgageAssociationBean;
import com.eostoken.eos_wallet.db.MgpWallet;
import com.eostoken.eos_wallet.mvp.mode.AppsModel;
import com.eostoken.eos_wallet.mvp.presenter.AppsPresenterImpl;
import com.eostoken.eos_wallet.mvp.view.AppsView;
import com.eostoken.eos_wallet.ui.adapter.AssociationAdapter;
import com.eostoken.eos_wallet.utils.RSAUtils;
import com.eostoken.eos_wallet.utils.WalletUtils;
import com.qmuiteam.qmui.layout.QMUILayoutHelper;
import com.qmuiteam.qmui.layout.QMUILinearLayout;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.eostoken.eos_wallet.utils.Constants.MORTGAGE;

public class MangoAssociationActivity extends BaseActivity<AppsModel, AppsView, AppsPresenterImpl> implements AppsView {

    @BindView(R.id.topBar)
    QMUITopBar topBar;
    @BindView(R.id.stimulateTv)
    AppCompatTextView stimulateTv;
    @BindView(R.id.layout1)
    QMUILinearLayout layout1;
    @BindView(R.id.sharedStimulateTv)
    AppCompatTextView sharedStimulateTv;
    @BindView(R.id.layout2)
    QMUILinearLayout layout2;
    @BindView(R.id.teamStimulateTv)
    AppCompatTextView teamStimulateTv;
    @BindView(R.id.layout3)
    QMUILinearLayout layout3;
    @BindView(R.id.nodeStimulateTv)
    AppCompatTextView nodeStimulateTv;
    @BindView(R.id.layout4)
    QMUILinearLayout layout4;
    @BindView(R.id.shareIndexTv)
    AppCompatTextView shareIndexTv;
    @BindView(R.id.hierarchyIndexTv)
    AppCompatTextView hierarchyIndexTv;
    @BindView(R.id.lightNodeTv)
    AppCompatTextView lightNodeTv;
    @BindView(R.id.currencyValueTv)
    AppCompatTextView currencyValueTv;
    @BindView(R.id.marketValueTv)
    AppCompatTextView marketValueTv;
    @BindView(R.id.layout5)
    QMUILinearLayout layout5;
    @BindView(R.id.midSearchView)
    SearchView midSearchView;
    @BindView(R.id.contributionListTv)
    AppCompatTextView contributionListTv;
    @BindView(R.id.mango_recycler)
    RecyclerView mangoRecycler;

    private MgpWallet wallet;
    private String privateKey;
    private String accountName;
    private String code;
    private String action;
    private MortgageAssociationBean mortgageAssociationBean;
    private int mRadius;
    private AssociationAdapter associationAdapter;
    private String mQuery;
    private List<MortgageAssociationBean.DataBean.TeamListBean> originalList;
    private List<MortgageAssociationBean.DataBean.TeamListBean> newDataList = new ArrayList<>();

    @Override
    public int getContentView() {
        return R.layout.fragment_mango_association;
    }

    @Override
    protected void initData() {
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.app_color_blue));
        BarUtils.setStatusBarLightMode(this, false);
        wallet = WalletUtils.getCurrentWallet(MangoAssociationActivity.this);
        privateKey = wallet.getPrivateKey();
        accountName = wallet.getCurrentAccountName();
        mRadius = QMUIDisplayHelper.dp2px(MangoAssociationActivity.this, 10);
    }

    public void lazyLoad() {
        try {
            Map mapOrderIndex = MapUtils.newHashMap();
            mapOrderIndex.put("address", accountName);
            String jsonData2 = GsonUtils.toJson(mapOrderIndex);
            String content = RSAUtils.encrypt(jsonData2);
            presenter.requestPortData(content, MORTGAGE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initView() {
        topBar.setTitle(StringUtils.getString(R.string.str_mango_association_t));
        topBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        layout1.setRadius(mRadius);
        layout2.setRadius(mRadius);
        layout3.setRadius(mRadius);
        layout4.setRadius(mRadius);
        layout5.setRadius(mRadius, QMUILayoutHelper.HIDE_RADIUS_SIDE_BOTTOM);
        associationAdapter = new AssociationAdapter(newDataList);
        mangoRecycler.setLayoutManager(new LinearLayoutManager(this));
        mangoRecycler.setAdapter(associationAdapter);

        // 设置搜索文本监听
        midSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (!ObjectUtils.isEmpty(originalList)) {
                    for (int i = 0; i < originalList.size(); i++) {
                        MortgageAssociationBean.DataBean.TeamListBean teamListBean = originalList.get(i);
                        if (ObjectUtils.equals(mQuery, teamListBean.getCode())) {
                            newDataList.clear();
                            newDataList.add(teamListBean);
                        }
                    }
                }
                return false;
            }

            // 当搜索内容改变时触发该方法，时刻监听输入搜索框的值
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!ObjectUtils.isEmpty(newText)) {
                    mQuery = newText;//  newText输入搜索框的值
                } else {
                    newDataList.clear();
                    newDataList.addAll(originalList);
                }
                return false;
            }
        });
    }

    private void updataView() {
        if (mortgageAssociationBean != null) {
            MortgageAssociationBean.DataBean dataBean = mortgageAssociationBean.getData();
            if (dataBean != null) {
                stimulateTv.setText(ObjectUtils.isEmpty(dataBean.getYesterdayMoney()) ? "0.0000" : dataBean.getYesterdayMoney());
                sharedStimulateTv.setText(ObjectUtils.isEmpty(dataBean.getYesterdayPushMoney()) ? "0.0000" : dataBean.getYesterdayPushMoney());
                teamStimulateTv.setText(ObjectUtils.isEmpty(dataBean.getYesterdayTeamMoney()) ? "0.0000" : dataBean.getYesterdayTeamMoney());
                nodeStimulateTv.setText(ObjectUtils.isEmpty(dataBean.getYesterdayLightNodeMoney()) ? "0.0000" : dataBean.getYesterdayLightNodeMoney());

                shareIndexTv.setText(ObjectUtils.isEmpty(dataBean.getMyPushPro()) ? "0%" : dataBean.getMyPushPro());
                hierarchyIndexTv.setText(ObjectUtils.isEmpty(dataBean.getMyFloor()) ? "0" : dataBean.getMyFloor() + "");
                lightNodeTv.setText(ObjectUtils.isEmpty(dataBean.getLightNodeFlag()) ? getString(R.string.str_no) : (dataBean.getLightNodeFlag() == 1 ? getString(R.string.str_yes) : getString(R.string.str_no)));
                currencyValueTv.setText((ObjectUtils.isEmpty(dataBean.getTeamNum()) ? "0" : dataBean.getTeamNum()) + "MGP");
                marketValueTv.setText("$" + (ObjectUtils.isEmpty(dataBean.getTeamValue()) ? "0" : dataBean.getTeamValue()));
                originalList = dataBean.getTeamList();
                newDataList.clear();
                newDataList.addAll(originalList);
                associationAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void updateView(Object data, int responseSuccessCode) {
        switch (responseSuccessCode) {
            case MORTGAGE:
                if (data != null) {
                    mortgageAssociationBean = GsonUtils.fromJson(data.toString(), MortgageAssociationBean.class);
                    if (mortgageAssociationBean.getCode() == 0) {
                        updataView();
                    } else {
                        ToastUtils.showShort(mortgageAssociationBean.getMsg());
                    }
                }
                break;
        }
    }

    @Override
    public void onFailure(Object data, int responseFailCode) {

    }

    @Override
    public void updateView(boolean success, Object o1, Object o2) {

    }

    @Override
    public AppsModel createModel() {
        return new AppsModel();
    }

    @Override
    public AppsView createView() {
        return this;
    }

    @Override
    public AppsPresenterImpl createPresenter() {
        return new AppsPresenterImpl(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        lazyLoad();
    }

    @Override
    public boolean isShowState() {
        return true;
    }
}
